package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.lxj.xpopup.core.BottomPopupView;
import com.qiantu.api.entity.ControllerBrandProtocolBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

import java.util.ArrayList;
import java.util.List;

public class BottomInterruptSceneDialog extends BottomPopupView {

    private BottomInterruptSceneDialog.DialogClickListener mDialogClickListener;
    private WheelView wv_first;
    private WheelView wv_second;
    private ArrayWheelAdapter mFirstArrayWheelAdapter;
    private ArrayWheelAdapter mSecondArrayWheelAdapter;
    private List<SceneBean> mScenes;
    private SceneBean selectScene;

    public void setScene(SceneBean scene){
        mScenes = new ArrayList<>();
        mScenes.add(scene);
        List<SceneBean> subScenes = (List<SceneBean>) scene.getTemporary();
        selectScene = subScenes.get(0);
        if(wv_first != null){
            initWheelView();
        }
    }

    public BottomInterruptSceneDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_interrupt_scene;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        wv_first = findViewById(R.id.options1);
        wv_second = findViewById(R.id.options2);
        View btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick(selectScene);
                }
            }
        });
        setStyle();
        initWheelView();
    }

    public void setSelected(SceneBean sceneBean){
        this.selectScene = sceneBean;
        setCurrentItem();
    }

    private void setStyle() {
        wv_first.setGravity(Gravity.CENTER);
        wv_first.setCyclic(false);
        wv_first.setAlphaGradient(true);
        wv_first.setItemsVisibleCount(5);
        wv_first.setDividerColor(0x00000000);
        wv_first.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_first.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));

        wv_second.setGravity(Gravity.CENTER);
        wv_second.setCyclic(false);
        wv_second.setAlphaGradient(true);
        wv_second.setItemsVisibleCount(5);
        wv_second.setDividerColor(0x00000000);
        wv_second.setTextColorCenter(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        wv_second.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_18));
    }

    private void initWheelView(){
        if(mScenes == null){
            return;
        }
        mFirstArrayWheelAdapter  = new ArrayWheelAdapter(mScenes);
        wv_first.setAdapter(mFirstArrayWheelAdapter);
        setCurrentItem();

        wv_first.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                SceneBean sceneBean = mScenes.get(index);
                List<SceneBean> subScenes = (List<SceneBean>) sceneBean.getTemporary();
                mSecondArrayWheelAdapter = new ArrayWheelAdapter(subScenes);
                wv_second.setAdapter(mSecondArrayWheelAdapter);
                wv_second.setCurrentItem(0);
                selectScene = subScenes.get(0);
            }
        });
        wv_second.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                SceneBean sceneBean = (SceneBean) mSecondArrayWheelAdapter.getItem(index);
                selectScene = sceneBean;
            }
        });
    }

    private void setCurrentItem(){
        if(wv_first == null || wv_second == null){
            return;
        }
        List<SceneBean> subScenes = (List<SceneBean>) mScenes.get(0).getTemporary();
        mSecondArrayWheelAdapter  = new ArrayWheelAdapter(subScenes);
        wv_second.setAdapter(mSecondArrayWheelAdapter);
        if(selectScene != null){
            for(int i=0; i<subScenes.size(); i++){
                SceneBean sceneBean = subScenes.get(i);
                if(sceneBean.getSceneSerialNo().equals(selectScene.getSceneSerialNo())){
                    wv_second.setCurrentItem(i);
                    return;
                }
            }
        }
        wv_second.setCurrentItem(0);
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable BottomInterruptSceneDialog.DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    public interface DialogClickListener {
        void onConfirmClick(SceneBean sceneBean);
    }
}
